package android.support.v4.os;

import java.io.FileDescriptor;
import java.net.InetAddress;


/**
 * 
 * @author yichou 2015-3-20
 *
 */
public class EIoBridge extends EHook {

	@Override
	public String getClassName() {
		return "libcore.io.IoBridge";
	}
	
	// @formatter:off

	// public static void connect(FileDescriptor fd, InetAddress inetAddress, int port) throws SocketException
	// public static void connect(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs) throws SocketException, SocketTimeoutException
	// public static FileDescriptor open(String path, int flags) throws FileNotFoundException
	// public static FileDescriptor socket(boolean stream) throws SocketException
	// libcore/luni/src/main/java/libcore/io/IoBridge.java

	// @formatter:on
	
	@Override
	public void hook() {
		super.hook();
		
		findAndHookMethod("connect", FileDescriptor.class, InetAddress.class, int.class, int.class, this);
	}
	
	@Override
	protected boolean before(String name, Object[] args, MethodHookParam param) {
		if("connect".equals(name)) {
			InetAddress address = (InetAddress) param.args[1];
			int port = (Integer) param.args[2];
			String hostName = address.getHostAddress();
			try {
				hostName = address.getHostName();
			} catch (Throwable ignored) {
				hostName = address.toString();
			}
			
			log.i("IoBridge", name + " " + address + ":" + port);
			
//			if(Statics.hookLocalIp && hostName.startsWith(EInetAddress.sRealLocalIp)) {
//				try {
//					InetAddress newAddress = InetAddress.getByName(EInetAddress.sRealLocalIp);
//					param.args[1] = newAddress;
//				} catch (Exception e) {
//					log.e("IoBridge", "redirect local ip fail, " + e.getMessage());
//				}
//			}
		}
		
		return super.before(name, args, param);
	}
	
	@Override
	protected void after(String name, Object[] args, MethodHookParam param) {
		// TODO Auto-generated method stub
		super.after(name, args, param);
	}
}
